﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OpenNETCF.Net;

namespace OpenNETCF.Bookshare.Services.ISBNDB
{
    //         // http://isbndb.com/api/books.xml?access_key=12345678&index1=isbn&value1=0596002068 

    internal class IsbnDbRequestEngine
    {
        private string AccessKey = "ZPSUFWBC";

        public string BaseUrl { get; set; }

        public IsbnDbRequestEngine()
        {
            BaseUrl = "http://isbndb.com";
        }

        public IResponse ProcessRequest(IRequest request)
        {
            RestConnector connector = new RestConnector(BaseUrl);

            var cmd = new StringBuilder();

            request.Parameters.Add("access_key", AccessKey);

            bool first = true;
            foreach (var param in request.Parameters)
            {
                if (first)
                {
                    cmd.AppendFormat("{0}={1}", param.Name, param.Value);
                    first = false;
                }
                else
                {
                    cmd.AppendFormat("&{0}={1}", param.Name, param.Value);
                }
            }

            var response = connector.Get(string.Format("api/{0}?", request.ApiPage), cmd.ToString());

            return ParseResponse(request, response);
        }

        private IResponse ParseResponse(IRequest request, string responseData)
        {
            return request.GetParser().Parse(responseData);
        }
    }


}
